﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Athena.RemoteAddress.Implementations
{
    public abstract class BaseServiceAddressManager : IServiceAddressManager
    {

        protected EventHandler<ServiceAddressEventArgs> created;
        protected EventHandler<ServiceAddressEventArgs> removed;
        protected EventHandler<ServiceAddressEventArgs> changed;


        public BaseServiceAddressManager()
        {
        }

        public event EventHandler<ServiceAddressEventArgs> Created {
            add { created += value; }
            remove { created -= value; }
        }

        public event EventHandler<ServiceAddressEventArgs> Removed {
            add { removed += value; }
            remove { removed -= value; }
        }

        public event EventHandler<ServiceAddressEventArgs> Changed {
            add { changed += value; }
            remove { changed -= value; }
        }

        protected void OnCreated(params ServiceAddressEventArgs[] args)
        {
            if (created == null)
            {
                return;
            }
            foreach (var arg in args)
            {
                created(this, arg);
            }
        }

        protected void OnChanged(params ServiceAddressEventArgs[] args)
        {
            if (changed == null)
            {
                return;
            }
            foreach (var arg in args)
            {
                changed(this, arg);
            }
        }

        protected void OnRemoved(params ServiceAddressEventArgs[] args)
        {
            if (removed == null)
            {
                return;
            }
            foreach (var arg in args)
            {
                removed(this, arg);
            }
        }

        public abstract Task<IEnumerable<ServiceAddressDescriptor>> GetServiceAddressAsync();

        public abstract Task SetServiceAddressAsync(IEnumerable<ServiceAddressDescriptor> remoteAddressModel);

        public abstract Task ClearAsync();
    }
}
